<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <table>
        <tr>
            <td><label>标题：</label></td>
            <td><input type="text" name="title" ></td>
        </tr>
        <tr>
            <td><label>摘要：</label></td>
            <td><input type="text" name="digest" ></td>
        </tr>
        <tr>
            <td><label>内容：</label></td>
            <td><input type="text" name="content" ></td>
        </tr>
        <tr>
            <td><label>分类：</label></td>
            <td><input type="text" name="classify" ></td>
        </tr>
        <tr>
            <td><label>作者：</label></td>
            <td><input type="text" name="author" ></td>
        </tr>
        <tr>
            <td><input type="button" value="保存" onclick="save()"></td>
            <td><input type="button" value="取消" onclick="cancel()" ></td>
        </tr>
    </table>

    <script src="./js/jquery-3.6.0.min.js"></script>
    <script src="./config/baseUrl.js"></script>
    <script src="./js/api.js"></script>
    <script>

        $(function(){
            let id =getUrl('id');
            id =id || 0;
            getBlogById(id).then(res=>{
                let obj =res.data;
                $('[name=title]').val(obj.title);
                $('[name=digest]').val(obj.digest);
                $('[nam=content]').val(obj.content);
                $('[name=classify]').val(obj.classify);
                $('[name=author]').val(obj.author);
            })
        })
        function getUrl(name){
            var reg =RegExp("(^|&)" +name +"=([^&]*)(&|$)");
            var r =window.location.search.substr(1).match(reg);
            if(r!=null) return unescape(r[2]); return null;
        }
        function save(){
            let id =getUrl('id');
            let obj ={
                title:$('[name=title]').val(),
                digest:$('[name=digest]').val(),
                content:$('[name=content]').val(),
                classify:$('[name=classify]').val(),
                author:$('[name=author]').val()
            }
            if(id){
                updateBlog(id,obj).then(res=>{
                    location.href ='./index.html';
                })
            }else{
                let params ={
                    title:obj.title,
                    digest:obj.digest,
                    content:obj.content,
                    classify:obj.classify,
                    author:obj.author
                }
                addBlog(params).then(res=>{
                    location.href ='./index.html';
                })
            }
        }
        function cancel(){
            location.href ='./index.html';
        }
    </script>
</body>
</html>